<template>
  <!--基本信息  -->
  <!-- <div style="height:590px"> -->
    <div>
    <FormPanel
      ref="formPanel"
      :list="initForm"
      :cols="2"
      :formType="value.type"
      :labelWidth="110"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getUserBaseInfo, selectOneByProgramId } from '@/api/cms-api/projectOperating/program/distribution';
import { getInfoBase } from '@/api/cms-api/projectOperating/program/push';

export default {
  mixins: [dictionary],
  props: {
    value: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      initForm: this.initFormFun(),
      formValue: {},
      orgId: null //事业部ID
    };
  },
  mounted() {
    // debugger
    // console.log(this.value); //获取到行id 、类型（新增、详情）
    //请求接口进行赋值，自己写吧
    //如果传入参数不为空，则说明是修改进入，初始化对应数据
    if (this.value != null && (this.value.type == 'edit' || this.value.type == 'show')) {
      console.log('value的值2:', this.value);
      this.init();
    } else {
      //add
      //将事业部信息设置本用户对应的部门 :eg北上广
      const re = this.getUserBaseInfoFun();
    }
  },
  methods: {
    async getUserBaseInfoFun() {
      const re = await getUserBaseInfo(-9999999);

      this.initForm[4].initialValue = re.data.orgId.orgId;
      // this.initForm[5].initialValue = re.data.orgId.orgName;
    },
    //修改时，初始化页面数据
    async init() {
      // console.log("reFileUrl",this.value.rowData['projectProgramId']);
      // debugger
      var id = this.value.rowData['projectProgramId'];
      var orgId = this.value.rowData['orgId'];
      this.orgId = orgId;
      let param;
      param = Object.assign({}, param, { projectProgramId: id });
      param = Object.assign({}, param, { orgId: orgId });
      const re = await selectOneByProgramId(param);
      console.log('re123', re);
      var twoDate = [];
      if (re.data.projectStartDate != null && re.data.projectEndDate != null) {
        twoDate.push(re.data.projectStartDate);
        twoDate.push(re.data.projectEndDate);
      }
      //const re = await selectOneByProgramId(param);

      this.initForm.forEach(x => {
        if (x.fieldName == 'hdzq') {
          x.initialValue = twoDate;
        } else {
          x.initialValue = re.data[x.fieldName];
        }
      });

      // console.log("reFileUrl",re);
      // console.log("infoBase2:",this.initForm)
      // debugger

      //循环为表单赋值
      // this.initForm.forEach(x => {
      //   if(x.fieldName=='hdzq'){
      //     x.initialValue=twoDate;
      //     // x.initialValue[1] = this.formatDates(this.value.rowData['projectStartDate']);
      //     // x.initialValue[0] = this.formatDates(this.value.rowData['projectEndDate']);
      //     // console.log("infoBase1:",this.value.rowData,x.initialValue[0],x.initialValue[1])
      //   }

      // });

      //附件信息回显
    },
    formatDates: function(value) {
      //时间处理函数
      // 这里的 value 就是需要过滤的数据
      if (value == null || value == '') {
        return '';
      }
      var date = new Date(value);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      if (month < 10) {
        month = '0' + month;
      }
      var day = date.getDate();
      return year + '-' + month + '-' + day;
    },
    //搜索数据
    initFormFun() {
      return [
        {
          type: 'INPUT',
          label: '方案编号',
          fieldName: 'programCode',
          disabled: true,
          initialValue: '系统自动生成'
        },
        {
          type: 'SELECT',
          label: '方案状态',
          fieldName: 'programStatus',
          placeholder: '请选择',
          initialValue: 25281001,
          itemList: this.createDictList('2528'),
          disabled: true
        },
        {
          type: 'SELECT',
          label: '方案下发渠道',
          fieldName: 'programIssuedChannel',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('2530'),
          disabled: true
        },
        {
          type: 'RANGE_DATE',
          label: '项目周期',
          fieldName: 'hdzq',
          valueFormat: 'yyyy-MM-dd', //返回数据格式
          initialValue: [],
          disabled: true,
          placeholder: '请选择',
          style: { width: '100%' },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ]
        },
        {
          type: 'SELECT',
          label: '事业部',
          fieldName: 'orgName',
          placeholder: '请选择',
          initialValue: '',
          disabled: true,
          itemList: this.createDictList('2566')
        },

        {
          type: 'TEXT_AREA',
          label: '方案名称',
          fieldName: 'programName',
          rows: 2,
          initialValue: '',
          disabled: true,
          placeholder: '请输入',
          rules: [{ required: true, message: '必填', trigger: 'blur' }, { min: 1, max: 90, message: '长度在 1 到 90 个字符', trigger: 'blur' }] //验证
        },
        {
          type: 'TEXT_AREA',
          label: '方案描述',
          fieldName: 'programDesc',
          disabled: true,
          rows: 3,
          initialValue: '',
          placeholder: '请输入',
          rules: [{ required: true, message: '必填', trigger: 'blur' }, { min: 1, max: 300, message: '长度在 1 到 300 个字符', trigger: 'blur' }] //验证
        },
        {
          type: 'TEXT_AREA',
          label: '事业部描述',
          fieldName: 'distributionDesc',
          rows: 3,
          initialValue: '',
          placeholder: '请输入',
          rules: [{ required: true, message: '必填', trigger: 'blur' }] //验证
        },
        {
          type: 'UPLOAD_FILE',
          label: '事业部附件',
          fieldName: 'fileUrl',
          disabled: true,
          upload: {
            actionUrl: '/api/file/oss/upload', //上传的地址
            fileTypes: ['pdf', 'jpg', 'png', 'rar']
          },
          initialValue: '',
          selfCols:2
        }
      ];
    },
    formChangeHandle(val) {
      //val.programIssuedChannel = '25301002'; //事业部下发代码
      // debugger
      this.formValue = val;
      this.$emit('childByValue', true);
      this.$emit('formSave');
    }
  }
};
</script>
